var charpterIds = null;
$(function () {
    var iSBLId = $('[name=iSBLId]').val();
    var form = new xhForm($('#xh_form'));
    form.parse();
    $('.form-date').datetimepicker({format: 'yyyy-mm-dd',language: 'zh-CN',minView: "month", autoclose:true});

    $('[name=iGrade],[name=iTchId],[name=iChargeId],[name=sBookId],[name="sChapterIds[]"]').select2();
    $('[name=iGrade]').on('change',function () {
        var gradeId = $(this).val();
        xhExtra.ajaxRequest('school_book/getSubject',{gradeId:gradeId},{},function (res) {
            var subjectDom = $('[name=iSubject]');
            subjectDom.empty();
            subjectDom.append(' <option value="" selected style="display: none">请选择</option>');
            $.each(res,function (i,text) {
                subjectDom.append(' <option value="'+i+'" >'+text+'</option>');
            })
        })
    });

    $('[name=iSubject]').on('change',function () {
        var gradeId = $('[name=iGrade]').val();
        var subjectId = $(this).val();
        xhExtra.ajaxRequest('school_book/getBook',{gradeId:gradeId,subjectId:subjectId,iSBLId:iSBLId},{},function (res) {
            if(res.catalogs != null){
                $('[name=catalogs]').val(JSON.stringify(res.catalogs));
            }
            if(res.books != null){
                var bookDom = $('[name=sBookId]');
                bookDom.empty();
                bookDom.append(' <option value="" selected style="display: none">请选择</option>');
                $.each(res.books,function (id,text) {
                    bookDom.append(' <option value="'+id+'" >'+text+'</option>');
                })
            }
            bookDom.select2();
        })

    });
    $('[name=sBookId]').on('change',function () {
        var bookId = $(this).val();
        var catalogs = jQuery.parseJSON($('[name=catalogs]').val());
        var catalogDom = $('[name="sChapterIds[]"]');
        catalogDom.empty();
        $.each(catalogs[bookId],function (i,item) {
            catalogDom.append(' <option value="'+item['catalogId']+'" >'+item['catalogName']+'</option>');
        });
        catalogDom.select2();
    });

    $('[name="sChapterIds[]"]').on('change',function () {
        var bookId = $('[name=sBookId]').val();
        var chapters = $(this).val();
        xhExtra.ajaxRequest('school_book/getRealNum',{bookId:bookId,chapters:chapters},{},function (res) {
            $('[name=iRealNum]').val(res.total);
        });
    });

    if(info != null){
        form.fill(info);
        $('[name=iGrade]').val(info['iGrade']).trigger('change');
        $('[name=iChargeId]').val(info['iChargeId']).trigger('change');
        $('[name=iTchId]').val(info['iTchId']).trigger('change');
        setTimeout(function () {
            $('[name=iSubject]').val(info['iSubject']).trigger('change');
            setTimeout(function () {
                $('[name=sBookId]').val(info['sBookId']).trigger('change');
                $('[name="sChapterIds[]"]').val(info['sChapterIds'].split(',')).trigger('change');
            },500);
        },500);
    }

    //全部选中
    $('#allSelect').click(function () {
        var bookId =  $('[name=sBookId]').val();
        var catalogs = jQuery.parseJSON($('[name=catalogs]').val());
        if (!bookId || !catalogs) {
            window.top.xhTop.showError('请先选择书本');
            return false;
        }
        var arr = [];
        $.each(catalogs[bookId],function (i,item) {
            arr.push(item['catalogId']);
        });
        $('[name="sChapterIds[]"]').val(arr).trigger('change');
    });

    $('#bt_modal_confirm',parent.document).click(function () {
        form.submit('school_book/saveLog',{},function (res) {
            window.top.xhTop.showSuccess(res.msg);
            window.top.xhTop.closeModal();
            window.top.xhTop.getActiveFrame().frames['tabFrame']['pushLogFrame'].location.reload();
        })
    });

});